package net.entelijan.cobean.examples.showcase.orderlist;

import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.List;

import javax.swing.JOptionPane;

import net.entelijan.cobean.core.ColumnDesc;
import net.entelijan.cobean.core.ICobean;
import net.entelijan.cobean.data.literature.ILitService;
import net.entelijan.cobean.data.literature.LitBook;
import net.entelijan.cobean.examples.panels.DefaultOrderlistPanel;
import net.entelijan.cobean.examples.util.ExamplesUtil;
import net.entelijan.cobean.orderlist.DefaultOrderlistInitializer;
import net.entelijan.scf.IShowcase;

public class MultiSelectionOrderlistShowcase implements
		IShowcase<MultiSelectionOrderlistPanel> {

	private ILitService service = null;

	public MultiSelectionOrderlistShowcase() {
		super();
	}

	@Override
	public void init(final MultiSelectionOrderlistPanel panel) {
		DefaultOrderlistPanel orderlistPanel = panel.getOrderlistPanel();

		DefaultOrderlistInitializer<LitBook> ini = new DefaultOrderlistInitializer<LitBook>();
		ini.setColumnDescs(ColumnDesc.createPropertyOnlyColumnDescs("title",
				"subtitle", "author.name", "year"));

		ini.setCoList(orderlistPanel.getList());
		ini.setCoUpButton(orderlistPanel.getUpButton());
		ini.setCoDownButton(orderlistPanel.getDownButton());
		// No selection mode has to be defined because MULTIPLE_INTERVAL is the
		// default

		// Initialize the components and receive the component bean.
		// From now on the component bean is your interface to the 'real'
		// components
		final ICobean<List<LitBook>> cobean = ini.initialize();

		cobean.setValue(ExamplesUtil.filterBooksByYear(service.loadAllBooks(),
				1900, 1970));

		panel.getShowListButton().addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent e) {
				JOptionPane.showMessageDialog(panel, ExamplesUtil.listToString(
						cobean.getValue(), 80));
			}

		});
	}

	public ILitService getService() {
		return service;
	}

	public void setService(ILitService service) {
		this.service = service;
	}

}
